/**
 * 供应商选择控件
 */
CustomerSelect = function(config) {
	this.url = config.url;
	this.width = config.width;
	this.windowWidth = config.windowWidth || 500;
	this.windowHeight = config.windowHeight || 300;
	this.avgWidth = (this.windowWidth - 89) / 3;
	this.inputId = config.inputId;
	this.windowTitle = config.title;
	this.multiple = config.multiple;
	this.viewType = 'list';
	this.selected = config.selected;
	this.init();
}

CustomerSelect.prototype = {
	init : function() {
		var selectEntity = this;
		var inputHidden = $('#' + this.inputId);

		var content_div = $('<DIV></DIV>');
		inputHidden.after(content_div);

		this.link_div = $('<DIV class="link_content"></DIV>');
		content_div.append(this.link_div);
		this.link_div.width(this.width - 19);

		this.link_hider = $('<DIV class="link_content_hider"></DIV>');
		this.link_hider.appendTo(this.link_div);
		this.link_hider.width(this.width - 19);
		this.link_inner = $('<DIV class="link_content_inner"></DIV>');
		this.link_inner.css('width', 1000);
		this.link_inner.appendTo(this.link_hider);

		var but = $('<DIV class="customerSelectButton"></DIV>');
		this.link_div.after(but);

		but.click(function() {
			selectEntity.openDataSelectWindow();
		}).mousedown(function() {
			$(this).addClass('customerSelectButtonDown').mouseout(function() {
				$(this).removeClass('customerSelectButtonDown');
			});
		}).mouseup(function() {
			$(this).removeClass('customerSelectButtonDown');
		});
		if (this.selected) {
			$.ajax({
				url : projectName + '/system/ajax/findCustomerByIds.t4m',
				data : 'ids=' + this.selected,
				success : function(json) {
					selectEntity.initChoose(json);
				}
			});
		}
	},
	openDataSelectWindow : function() {
		var customerSelectEntity = this;
		this.customerPanel = new WindowPanel(
				{
					id : 'customerSelect' + this.inputId,
					title : this.windowTitle,
					width : this.windowWidth,
					height : this.windowHeight,
					maximizable : false,
					draggable : false,
					html : this.showData(),
					tbar : new Toolbar(
							{
								id : '__customer_select',
								icon : '../../image/op.gif',
								items : [
										{
											id : 'customerFilter',
											type : 'textfield',
											width : 80,
											keyup : function() {
												customerSelectEntity.customerPanel.jContent
														.html(customerSelectEntity
																.showData('customerName='
																		+ $(
																				'#customerFilter')
																				.val()));
												customerSelectEntity
														.itemEvent();
											}
										},
										'-',
										{
											type : 'button',
											text : '确定',
											position : {
												a : '-80px -100px'
											},
											handler : function() {
												customerSelectEntity.ok();
											}
										}, '-', {
											type : 'button',
											text : '新增客户',
											position : {
												a : '-80px -100px'
											},
											handler : function() {
												top.customer.add();
											}
										} ]
							})
				});

		if (this.multiple) {
			this.customerPanel.tbar.add({
				type : 'button',
				text : '全选',
				position : {
					a : '-60px -100px'
				},
				handler : function() {
					customerSelectEntity.chooseAll();
				}
			});
		}
		this.customerPanel.tbar.add({
			type : 'button',
			text : '清空',
			position : {
				a : '0px -20px'
			},
			handler : function() {
				customerSelectEntity.clear();
			}
		});
		this.itemEvent();
		$(
				'#jWindowPanel-customerSelect' + customerSelectEntity.inputId
						+ ' .headimg').load(function() {
			customerSelectEntity.rebuildPosition($(this));
		});
	},
	rebuildPosition : function(_m) {
		var pSize = this.viewType === 'grid' ? 54 : 20;
		var iSize = this.viewType === 'grid' ? 50 : 16;
		if (_m.height() < _m.width()) {
			_m.css('margin-top', (pSize - _m.height()) / 2);
		}
	},
	itemEvent : function() {
		var customerSelectEntity = this;
		// 获得所有选项元素
		var items = $('.content-body-item-' + this.viewType);
		// 添加hover事件
		items.hover(function() {
			$(this).addClass('content-body-item-hover');
		}, function() {
			$(this).removeClass('content-body-item-hover');
		});
		// 添加点击事件
		if (this.multiple) {
			items.click(function() {
				var _i = $(this);
				if (_i.hasClass('content-body-item-click')) {
					_i.removeClass('content-body-item-click');
					_i.children('.selected').remove();
				} else {
					_i.addClass('content-body-item-click');
					_i.append('<img src="' + projectName
							+ '/image/CustomerSelect/'
							+ customerSelectEntity.viewType
							+ '_selected.gif" class="selected"/>');
				}
			});
		} else {
			items.click(function() {
				var _i = $(this);
				$('.content-body-item-click').removeClass(
						'content-body-item-click').children('.selected')
						.remove();
				if (!_i.hasClass('content-body-item-click')) {
					_i.addClass('content-body-item-click');
					_i.append('<img src="' + projectName
							+ '/image/CustomerSelect/'
							+ customerSelectEntity.viewType
							+ '_selected.gif" class="selected"/>');
				}
			});
		}
		// 勾选已选中的
		var ids = document.getElementById(this.inputId).value.split(',');
		for ( var i = ids.length; i >= 0; i--) {
			if (ids[i]) {
				$('#' + ids[i]).addClass('content-body-item-click').append(
						'<img src="' + projectName + '/image/CustomerSelect/'
								+ customerSelectEntity.viewType
								+ '_selected.gif" class="selected"/>');
			}
		}
	},
	showData : function(params) {
		var customerSelectEntity = this;
		var tables = new Array();
		tables.push('<div class="customer-select-content">');
		$.ajax({
			url : this.url,
			data : params,
			cache : false,
			async : false,
			success : function(json) {
				var _customerList_ = json;
				var tempId = '';
				for ( var i = 0; i < _customerList_.length; i++) {
					tables.push('<div id="' + _customerList_[i].id
							+ '" class="content-body-item-'
							+ customerSelectEntity.viewType + '" title="'
							+ _customerList_[i].name + '">');
					tables.push('<div class="content-body-item-body">');
					tables.push('<div class="content-body-item-body-img">');
					tables.push('</div>');
					tables.push('<div class="content-body-item-body-info">');
					tables.push('<div style="width:'
							+ customerSelectEntity.avgWidth
							+ 'px;" class="info-bold">'
							+ _customerList_[i].name + '</div>');
					tables.push('<div style="width:'
							+ customerSelectEntity.avgWidth + 'px;">'
							+ _customerList_[i].phone + '</div>');
					tables.push('<div style="width:'
							+ customerSelectEntity.avgWidth + 'px;">'
							+ _customerList_[i].linkman + '</div>');
					tables.push('</div>');
					tables.push('</div>');
					tables.push('</div>');
				}
			}
		});
		tables.push('</div>');
		return tables.join('');
	},
	choose : function() {
		this.link_inner.html('');
		var list = $('.content-body-item-click');
		var values = new Array();
		var links = new Array();
		var customerSelectEntity = this;
		for ( var i = 0; i < list.length; i++) {
			var c_id = list.eq(i).attr('id');
			var c_name = list.eq(i).attr('title');
			values.push(c_id);
			var c_customer = this.getCustomerLink({
				customerId : c_id,
				customerName : c_name,
				showInfo : true
			});
			var c_html = $('<DIV id="' + this.inputId + '_' + c_id + '"></DIV>');
			c_html.append(c_customer[0]);
			c_html.append(c_customer[1]);
			this.link_inner.append(c_html);
		}
		$('#' + this.inputId).val(values.join(','));
		this.update();
	},
	initChoose : function(list) {
		this.link_inner.html('');
		var values = new Array();
		var links = new Array();
		var customerSelectEntity = this;
		for ( var i = 0; i < list.length; i++) {
			var c_id = list[i].id;
			var c_name = list[i].name;
			values.push(c_id);
			var c_customer = this.getCustomerLink({
				customerId : c_id,
				customerName : c_name,
				showInfo : true
			});
			var c_html = $('<DIV id="' + this.inputId + '_' + c_id + '"></DIV>');
			c_html.append(c_customer[0]);
			c_html.append(c_customer[1]);
			this.link_inner.append(c_html);
		}
		$('#' + this.inputId).val(values.join(','));
		this.update();
	},
	ok : function() {
		this.choose();
		WPS['jWindowPanel-customerSelect' + this.inputId].close();
	},
	chooseAll : function() {
		var list = $('.content-body-item-' + this.viewType);
		for ( var i = 0; i < list.length; i++) {
			if (!list.eq(i).hasClass('content-body-item-click')) {
				list.eq(i).addClass('content-body-item-click');
				list.eq(i).append(
						'<img src="' + projectName + '/image/CustomerSelect/'
								+ this.viewType
								+ '_selected.gif" class="selected"/>');
			}
		}
	},
	clear : function() {
		$('.content-body-item-click').removeClass('content-body-item-click')
				.children('.selected').remove();
	},
	update : function() {
		if (this.multiple) {
			// 判断是否溢出
			var linkas_widths = 0;
			this.link_inner.children('DIV').each(function() {
				linkas_widths += $(this).outerWidth(true);
			});
			if (linkas_widths > this.link_div.width()) {
				if (!this.hasMore) {
					var customerNames = [];
					this.link_inner.find('.customer_selected').each(function() {
						customerNames.push($(this).text());
					});
					this.link_div
							.append((this.link_fix = $('<div class="more" title="'
									+ customerNames.join(',') + '">...</div>')));
					this.hasMore = true;
				}
				this.link_hider.width(this.link_div.width()
						- this.link_fix.outerWidth());
			} else {
				this.link_hider.width(this.link_div.width());
				if (this.hasMore) {
					this.link_fix.remove();
					this.hasMore = false;
				}
			}
		}
	},
	removeCustomer : function(uid) {
		var hInput = $('#' + this.inputId);
		hInput.val(hInput.val().replace(uid, '').replace(',,', ','));
		$('#' + this.inputId + '_' + uid).remove();
		this.update();
	},
	getCustomerLink : function(config) {
		var entity = this;
		var showInfo = config.showInfo ? 'void(0);' : 'void(0);';
		var customer = $('<div class="customer_selected" onclick="' + showInfo
				+ '">' + config.customerName + '</div>');
		var c_delete = $('<div title="取消\'' + config.customerName
				+ '\'" class="closer"></div>');
		c_delete.click(function() {
			entity.removeCustomer(config.customerId);
		});
		return [ customer, c_delete ];
	}
}